/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.wbs;

import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.Button;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;

import com.yst.metho.web.application.projectversion.views.ActivityPane;
import com.yst.metho.web.application.projectversion.views.ProjectWBSListModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.services.exceptions.BusinessErrorCode;
import com.yst.suivi.services.exceptions.BusinessException;

/**
 * @author perso
 * 
 */
public class ModifyActivityBtn extends Button implements ActionListener
{
	private ProjectWBSListModel mModel;

	private ActivityPane mView;

	/**
	 * @param model
	 * @param view
	 */
	public ModifyActivityBtn(ProjectWBSListModel model, ActivityPane view)
	{
		super("Ok");
		mModel = model;
		mView = view;
		addActionListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see nextapp.echo2.app.event.ActionListener#actionPerformed(nextapp.echo2.app.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent arg0)
	{
		try
		{
			mView.getDuree();

			if (null == mView.getId())
			{

				try
				{
					Locator.getActivityView().createActivity(mView.getNom(), mView.getDuree(), mView.getClassification(), mView.getVersion(), null, mView.getTeamRole(), mView.getLot());
					mModel.refresh();
				}
				catch (NamingException e)
				{
					throw new RuntimeException(e);
				}
				catch (CreateException e)
				{
					throw new RuntimeException(e);
				}
			}
			else
			{
				try
				{
					Locator.getActivityView().storeActivity(mView.getId(), mView.getNom(), mView.getDuree(), mView.getClassification(), null, mView.getTeamRole(), mView.getLot());
					mModel.refresh();
				}
				catch (NamingException e)
				{
					throw new RuntimeException(e);
				}
				catch (CreateException e)
				{
					throw new RuntimeException(e);
				}
				catch (FinderException e)
				{
					throw new RuntimeException(e);
				}
			}
		}
		catch (NumberFormatException t)
		{
			String[] params1 = new String[0];
			String[] params2 =
			{ "duree" };
			throw new BusinessException(BusinessErrorCode.FIELD_SHOULD_BE_NUMBER, params1, params2);
		}
	}

}
